﻿
var career = {};
career.action = "create";
career.curCareer = -1;
career.createHtml = "";
career.initCreateBox = function (cobject) {
    var city = $("select[name='city']");
    var province = $("select[name='province']");
    var company = $("#company");
    var remark = $("#remark");
    var selCompany = $("#selCompany");
    if (cobject) {
        var start = $("select[name='start']");
        var end = $("select[name='end']");
        selCompany.val(cobject.isvisible);
        company.val(cobject.cname);
        remark.val(cobject.remark);
        setTimeout(function () { start.val(cobject.start); end.val(cobject.end) }, 1);
    }
    App.BindCityCtrl(province, "province", 1, function () {
        if (cobject) { setTimeout(function () { province.val(cobject.province); }, 1); }
        App.BindCityCtrl(city, "City", cobject ? cobject.province : province.val(), function () {
            if (cobject) { setTimeout(function () { city.val(cobject.city); }, 1); }
        }, "/Ajax/GetCity/");
        province.hide();
        city.hide();
        province.show();
        city.show();
        setTimeout(function () { App.Setup.BindSelect(selCompany); }, 1);
    }, "/Ajax/GetCity/");
    province.change(function () {
        App.BindCityCtrl(city, "City", province.val(), null, "/Ajax/GetCity/");
    }, "/Ajax/GetCity/");
    App.Setup.BindInput(company, "请输入名称，25个字以内", App.Setup.checkCompanyName);
    App.Setup.BindInput(remark, "可以填写部门、组别、职位等信息，70个字以内", App.Setup.checkRemark);
    $("#save").click(function () { career.submit(this); });
    App.autoComplete(company, function (id, name) { company.val(name) });
}
career.submit = function (alink) {
    var city = $("select[name='city']");
    var province = $("select[name='province']");
    var company = $("#company");
    var remark = $("#remark");
    var selCompany = $("#selCompany");
    var careerList = $("#careerList");
    App.Setup.checkCompanyName(company);
    App.Setup.checkRemark(remark);
    if (App.Setup.isCompanyName && App.Setup.isRemark && !alink.locked) {
        alink.locked = true;
        $.ajax(
       {
           url: this.action == "create" ? "/Ajax/CreateCareer" : "/Ajax/EditCareer",
           datatype: "json",
           cache: false,
           data: { careerid: career.curCareer, companyName: $.trim(company.val()), remark: remark.val(), isVisible: selCompany.val(), location: city.val() > 0 ? city.val() : province.val(), startdate: $("#start").val(), enddate: $("#end").val() },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               }
               else {
                   App.FullMiniTip(CodeList[o.Code], alink, 1000, 3);
                   if (career.action == "create") {
                       if (o.Data != "") {
                           var first = careerList.find("div:first");
                           if (first[0])
                               $(o.Data).insertBefore(first);
                           else
                               careerList.html(o.Data);
                       }
                   }
                   else {
                       if (o.Data != "") {
                           curCarItem = $("#career_" + career.curCareer)
                           $(o.Data).insertBefore(curCarItem);
                           curCarItem.remove();
                       }
                   }
               }
               alink.locked = false;
           },
           error: function (request) {
               App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               alink.locked = false;
           }
       });
    }
}
career.del = function (id) {
    App.confirm("是否真的删除？", function () {
        $.ajax(
       {
           url: "/Ajax/DelCareer",
           datatype: "json",
           cache: false,
           data: { careerid: id },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.alert(CodeList["A00003"]);
               }
               else {
                   $("#career_" + id).remove();
               }
           },
           error: function (request) {
               App.alert(CodeList["A00003"]);
           }
       });
    });

}
career.edit = function (id) {
    $.ajax(
       {
           url: "/Ajax/getCareerDetail",
           datatype: "json",
           cache: false,
           data: { careerid: id },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.alert(CodeList["A00003"]);
               }
               else {
                   $("div[edit='1']").html("");
                   var editElem = $("#careerEdit_" + id);
                   $("#divCreate").hide().find(".info_tab01").remove();
                   editElem.html("<div class='info_tab01'>" + career.createHtml + "</div>");
                   $("#btnCreate").show();
                   career.initCreateBox(o.Data);
                   career.action = "edit";
                   career.curCareer = id;
               }
           },
           error: function (request) {
               App.alert(CodeList["A00003"]);
           }
       });

}
$(function () {

    career.createHtml = $("#divCreate .info_tab01").html();
    career.initCreateBox();
    App.TimerFunArray.push(App.autoCompleteTimerEvent);
    App.curGetLink = "autoCompleteCompany";
    App.StartTimer();
    $("#btnCreate a").click(function () {
        $("div[edit='1']").html("");
        $("#divCreate").show().append("<div class='info_tab01'>" + career.createHtml + "</div>");
        career.initCreateBox();
        $("#btnCreate").hide();
        career.action = "create";
        career.curCareer = -1;
    });
});
   